'use strict';
var utils = require('./common_utils');
var cache = require('./common_cache');
 
// Cache of medecins
exports.data = {
   "medecins": [
  ]
};

/**
 * This method generates a json object that will be used as an index.
 * This index will be used as comparator and to retrieve the corresponding medecins.
 */
exports.getJsonIndexForMedecin = function(nom,prenom,id) 
{
	return cache.getJsonIndex(exports.data.medecins,utils.MEDECINS_FOLDER,nom,prenom,id);
}

exports.getIndexInMedecinList = function(medecinIndex) 
{
	return cache.getPositionInDATAList(exports.data.medecins,medecinIndex);
}

exports.getFilteredMedecinList = function(elements) 
{
	elements = elements.map(function(element){return element = utils.convertValue(element)});	
	return cache.getFilteredDATAList(exports.data.medecins,elements);
}

exports.updateMedecin= function(medecinIndex){
	cache.updateDATA(exports.data.medecins,medecinIndex);
}

exports.fillCacheWithFile = function(file) {
	var objectJson = utils.parseFileName(file);
	if (objectJson) {
		objectJson = exports.getJsonIndexForMedecin(objectJson.nom, objectJson.prenom, objectJson.id);
		objectJson.path = file.Path;
	}
	exports.data.medecins.push(objectJson);
}


// Browsing functions
exports.browseAllMedecinFolders = function() {
	var files = utils.browseAllFolders(utils.MEDECINS_FOLDER);
	files.forEach(function(file)
	{
		exports.fillCacheWithFile(file);
	});
	return exports.data.medecins;
}

exports.removeMedecin = function(medecinIndex) {
	return cache.removeDATA(exports.data.medecins,medecinIndex);
}
